﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;

public struct SParam
{
    public String m_sName;
    public float m_fRate;
    public int m_nOffset;
    public int m_nNumber;

    public SParam(String sName, float fRate, int nOffset, int nNumber)
    {
        m_sName = sName;
        m_fRate = fRate;
        m_nOffset = nOffset;
        m_nNumber = nNumber;
    }
}

public struct SFile
{
    public float m_fRate;
    public String m_sFileName;

    public SFile(String sName, float fRate)
    {
        m_sFileName = sName;
        m_fRate = fRate;
    }
}

class CXMLReader : XmlTextReader
{
    String      m_sFileName;
    String      m_sFlightName;
    String      m_sPlaneName;
    int         m_nNumberPlane;
    int         m_nNumberFlight;
    DateTime    m_StartDateTime;
    DateTime    m_StopDateTime;
    List<SParam> m_ParamsList;
    List<SFile> m_BinaryFiles;
     
    public CXMLReader(String sFileName)
        : base(sFileName)
    {
        m_sFileName = sFileName;
        m_nNumberFlight = 1;
        m_ParamsList = new List<SParam>();
        m_BinaryFiles = new List<SFile>();
        ReadFlightName();
        ReadNumberPlaneAndFlight();
        ReadNamePlane();
        ReadFlightDateTime();
        ReadParams();
        ReadBinaryFiles();
        Close();
    }

    private void ReadFlightName()
    {
        ReadToFollowing("media");
        m_sFlightName = GetAttribute(1);
        //Console.WriteLine(m_sFlightName);
    }

    private void ReadNumberPlaneAndFlight()
    {
        for (; Name != "acTail" & Read(););
        m_nNumberPlane = ReadContentAsInt();
        //Console.WriteLine(m_nNumberPlane);

        m_sFlightName += "_acTail" + m_nNumberPlane.ToString();

        if (m_sFileName.IndexOf("No") != -1)
        {
            int nStartInd = m_sFileName.IndexOf("No");
            int nEndInd = m_sFileName.IndexOf(".");
            String sFlightNumber = m_sFileName.Substring(nStartInd + 2, nEndInd - nStartInd - 2);
            m_sFlightName += "_No" + sFlightNumber;
            m_nNumberFlight = int.Parse(sFlightNumber);
        }
    
    }

    private void ReadNamePlane()
    {
        for (; Name != "acType" & Read(););
        m_sPlaneName = Value;
       // Console.WriteLine(m_sPlaneName);
    }

    private void ReadFlightDateTime()
    {
        //Start date
        for (; Name != "startDateTime" & Read(); ) ;
        String sDateTime = Value;
        
        String[] tmp;
        tmp = sDateTime.Split('T');
        tmp[1] = tmp[1].Remove(8);
        String[] Date = tmp[0].Split('-');
        String[] Time = tmp[1].Split(':');
        
        m_StartDateTime = new DateTime(Convert.ToInt32(Date[0]), Convert.ToInt32(Date[1]),Convert.ToInt32(Date[2]),
                                       Convert.ToInt32(Time[0]), Convert.ToInt32(Time[1]), Convert.ToInt32(Time[2]));
        //Console.WriteLine(m_StartDateTime.ToString());

        //Stop date
        for (; Name != "stopDateTime" & Read(); ) ;
        sDateTime = Value;

        tmp = sDateTime.Split('T');
        tmp[1] = tmp[1].Remove(8);
        Date = tmp[0].Split('-');
        Time = tmp[1].Split(':');

        m_StopDateTime = new DateTime(Convert.ToInt32(Date[0]), Convert.ToInt32(Date[1]), Convert.ToInt32(Date[2]),
                                       Convert.ToInt32(Time[0]), Convert.ToInt32(Time[1]), Convert.ToInt32(Time[2]));
        //Console.WriteLine(m_StopDateTime.ToString());
    }

    private void ReadParams()
    {
        ReadToFollowing("parameter");
        int nNumber = 1;

        while (Name != "parameters")
        {
            for (; Name != "parameter"; Read()) ;
            String sName = GetAttribute("name");
            if (sName.Contains("--"))
            {
                string[] separator = new string[] { "--" };
                string[] tmpArr = sName.Split(separator,2, StringSplitOptions.RemoveEmptyEntries);
                sName = tmpArr[0];
            }

            for (; Name != "rate" & Read(); ) ;
            float fRate = ReadContentAsFloat();

            for (; Name != "offsetMatrix" & Read(); ) ;
            int nOffset = Convert.ToInt16(Value.ToString());

            m_ParamsList.Add(new SParam(sName, fRate, nOffset, nNumber));
            //Console.WriteLine(" Name {0}\n rate {1}\n offset {2}\n Number {3}\n", sName, fRate, nOffset, nNumber);
            
            for (; Name != "parameter" & Read(); ) ;
            Read();
            ++nNumber;
        }
    }

    private void ReadBinaryFiles()
    {
        while (ReadToFollowing("fileRef"))
        {
            string sRate = GetAttribute("rate");
            float fRate;
            float.TryParse(sRate,
                            NumberStyles.Float,
                            CultureInfo.InvariantCulture,
                            out fRate);
            Read();
            String sName = Value;
            //Console.WriteLine(fRate);
            //Console.WriteLine(sName);

            SFile File = new SFile(sName, fRate);
            m_BinaryFiles.Add(File);
        }
    }

    public List<SParam> GetParamsList()
    {
        return m_ParamsList;
    }

    public List<SFile> GetBinaryFiles()
    {
        return m_BinaryFiles;
    }

    public String GetFlightName()
    {
        return m_sFlightName;
    }

    public String GetPlaneName()
    {
        return m_sPlaneName;
    }

    public int GetNumberPlane()
    {
        return m_nNumberPlane;
    }

    public DateTime GetStartDate()
    {
        return m_StartDateTime;
    }

    public DateTime GetStopDate()
    {
        return m_StopDateTime;
    }
}

